/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-11-22 16:19:19
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2024-06-04 15:52:27
 * @FilePath: \cxl-driving-training\src\views\column\list\index.ts
 */
import { BaseData, BaseMethod } from '@/utils/basc-data'
import { tableConfig } from './config'
import { reactive, ref } from 'vue'
import { dictAdd, dictDetele, dictEdit, dictList, dictPageProps } from '@/apis/system/dict'
import router from '@/router'
import { ElMessageBox, FormInstance } from 'element-plus'
import { DICT_DATA } from '@/utils/dict-data'

export default class DicttData extends BaseData implements BaseMethod {
    searchData = {
        key: ''
    }
    dialog = reactive({
        visible: false,
        title: '新增字典',
        type: 'create'
    })
    dictForm = reactive({
        name: '',
        key: '',
        desc: '',
        id: '',
        type: '1' //字典类型 1后台管理，2小程序
    })
    dictRules = {
        name: [{ required: true, message: '请输入字典名称', trigger: 'blur' }],
        key: [{ required: true, message: '请输入字典编码', trigger: 'blur' }],
        type: [{ required: true, message: '请选择字典类型', trigger: 'change' }]
    }
    constructor() {
        super(tableConfig)
        this.getData()
    }
    getData = (arg?: any) => {
        const data: dictPageProps = {
            current: this.pageData.pagination.pageNo,
            size: this.pageData.pagination.pageSize
        }
        if (this.searchData.key) {
            data.dictCode = this.searchData.key
        }
        dictList(data).then(res => {
            if (Number(res.data.code) === 200) {
                this.pageData.table.data.length = 0
                this.pageData.pagination.total = res.data.result.total

                const arr: any[] = res.data.result.records

                arr.forEach((element: any) => {
                    this.pageData.table.data.push(element)
                })
            }
        })
    }
    onSearch = (arg?: any) => {
        console.log(arg)
        this.searchData = arg
        this.getData()
    }
    onReset = (arg?: any) => {
        this.searchData = {
            key: ''
        }
        this.getData()
    }
    onPaginationChange = (arg?: any) => {
        this.pageData.pagination.pageNo = arg
        this.getData()
    }
    onPaginationSize = (arg?: any) => {
        this.pageData.pagination.pageNo = 1
        this.pageData.pagination.pageSize = arg
        this.getData()
    }
    onOperation = (arg?: any) => {
        const { type, data } = arg
        console.log(type, data)
        if (type === 'create') {
            this.dialog.visible = true
            this.dialog.title = '新增字典'
            this.dialog.type = type
            this.dictForm.id = ''
            this.dictForm.type = '1'
            this.dictForm.desc = ''
            this.dictForm.key = ''
            this.dictForm.name = ''
        } else if (type === 'edit') {
            this.dialog.visible = true
            this.dialog.title = '编辑字典'
            this.dialog.type = type
            this.dictForm.id = data.id
            this.dictForm.type = data.dictType + ''
            this.dictForm.desc = data.description
            this.dictForm.key = data.dictCode
            this.dictForm.name = data.dictName
        } else if (type === 'detail') {
            router.push({
                path: '/dict-details',
                query: {
                    id: data.id,
                    dictType: data.dictType
                }
            })
        } else if (type === 'delete') {
            ElMessageBox.confirm('确认要删除该字典?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    dictDetele(data.id).then(res => {
                        if (Number(res.data.code) === 200) {
                            DICT_DATA.getDictData(true)
                            this.getData()
                        }
                    })
                })
                .catch(() => {
                    //
                })
        } else if (type === 'switch') {
            //
        }
    }
    //监听弹窗关闭
    onCloseDialog = (formEl: FormInstance | undefined) => {
        if (!formEl) return
        this.dialog.visible = false
        formEl.resetFields()
    }
    //监听弹窗点击确定按钮
    submitForm = async (formEl: FormInstance | undefined) => {
        if (!formEl) return
        await formEl.validate((valid, fields) => {
            if (valid) {
                console.log('submit!')
                if (this.dialog.type === 'edit') {
                    dictEdit({
                        description: this.dictForm.desc,
                        dictName: this.dictForm.name,
                        dictCode: this.dictForm.key,
                        dictType: Number(this.dictForm.type),
                        id: this.dictForm.id
                    }).then(res => {
                        if (Number(res.data.code) === 200) {
                            this.dialog.visible = false
                            formEl.resetFields()
                            this.getData()
                            DICT_DATA.getDictData(true)
                        }
                    })
                } else {
                    dictAdd({
                        description: this.dictForm.desc,
                        dictName: this.dictForm.name,
                        dictCode: this.dictForm.key,
                        dictType: Number(this.dictForm.type)
                    }).then(res => {
                        if (Number(res.data.code) === 200) {
                            this.dialog.visible = false
                            formEl.resetFields()
                            this.getData()
                            DICT_DATA.getDictData(true)
                        }
                    })
                }
            } else {
                console.log('error submit!', fields)
            }
        })
    }
}
